package weibo

import (
	"encoding/json"
	"errors"
	"log/slog"
	"strconv"

	"gitee.com/gxcc/go-utils/utils"
)

type UserInfo struct {
	Id              int64  `json:"id"`
	Name            string `json:"name"`
	ScreenName      string `json:"screen_name"`
	Gender          string `json:"gender"`
	ProfileImageUrl string `json:"profile_image_url"`
	AvatarLarge     string `json:"avatar_large"`
	Location        string `json:"location"`
}

// GetUserInfo 获取授权的用户信息
func GetUserInfo(accessToken string, uid int64) (*UserInfo, error) {
	url := "https://api.weibo.com/2/users/show.json?access_token=" +
		accessToken + "&uid=" + strconv.FormatInt(uid, 10)
	res, err := utils.HttpGet(url, nil)
	if err != nil {
		slog.Error(err.Error())
		return nil, err
	}

	var weiboUser UserInfo
	if err := json.Unmarshal(res.Body, &weiboUser); err != nil {
		slog.Error(err.Error())
		return nil, err
	}
	if weiboUser.Id == 0 {
		return nil, errors.New(string(res.Body))
	}
	return &weiboUser, nil
}
